const db = require("../db/mysql");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken")
const {jwtSecret,expiresIn}  = require("../config");
const { brotliDecompress } = require("zlib");
const getArtcateNotDel = "select * from ev_artical_cate where isDelete=0 order by id asc";
const artcateIsUsed = "select * from ev_artical_cate where name=? or alias=?";
const insertArticalCateSql = "insert into ev_artical_cate set ?";
const delArticalCateSql = "update ev_artical_cate set isDelete=1 where id=?";
const getArticalByIdSql = "select * from ev_artical_cate where id=?";
// 查重
const isExitArticalSql = "select * from ev_artical_cate where id<>? and (name=? or alias=?)";
// 更新
const updateArtcateByIdSql =  "update ev_artical_cate set ? where id=?";
/**
 * 获取文章分类列表
 */
exports.getArtcate = (req,res)=>{
    db.query(getArtcateNotDel,(err,result)=>{
        if (err) {
            return res.cc(err)
        }
        const arrayToTree = (arr, pid) => {
            return arr.reduce((res, current) => {
              if (current['pid'] === pid) {
                current.children = arrayToTree(arr, current['id']);
                return res.concat(current);
              }
              return res;
            }, []);
          };
        let ress =  arrayToTree(result,0)
        // 执行成功
        res.send({
            status:0,
            message:"获取文章分类列表成功！",
            data:ress
        })
        return
    })
}
/**
 * 通过id获取文件分类
 * @param {*} req 
 * @param {*} res 
 */
exports.getArtcateById = (req,res)=>{
    db.query(getArticalByIdSql,req.params.id,(err,result)=>{
        if (err) {
            return res.cc(err)
        }
        if (result.length !== 1) {
            return res.cc("获取文章分类失败，请稍后再试")
        }
        // 执行成功
        res.send({
            status:0,
            message:"获取文章分类成功！",
            data:result[0]
        })
    })
}
/**
 * 新增文章分类
 */
exports.addArtcate = (req,res)=>{
    db.query(artcateIsUsed,[req.body.name,req.body.alias],(err,result)=>{
        if (err) {
            return res.cc(err)
        }
        // length == 2
        if (result.length==2) {
            return res.cc("分类名称和分类别名被占用，请更换后重试！");
        }
        if (result.length==1&&result[0].name==req.body.name&&result[0].alias==req.body.alias) {
            return res.cc("分类名称和分类别名被占用，请更换后重试！");
        }
        if (result.length==1&&result[0].name==req.body.name) {
            return res.cc("分类名称被占用，请更换后重试")
        }
        if (result.length==1&&result[0].alias==req.body.alias) {
            return res.cc("分类别名被占用，请更换后重试")
        }
        db.query(insertArticalCateSql,req.body,(err,result)=>{
            console.log(req.body);
            if (err) {
                return res.cc(err)
            }
            if (result.affectedRows !== 1) {
                return res.cc("新增文章分类失败，请稍后再试")
            }
            return res.send({
                status:0,
                message:"新增文章分类成功！",
            })
        })
    })
}
/**
 * 删除文章分类
 */
exports.delArtcateById=(req,res)=>{
    db.query(delArticalCateSql,req.params.id,(err,result)=>{
        if (err) {
            return res.cc(err)
        }
        if (result.affectedRows !== 1) {
            return res.cc("删除文章分类失败，请稍后再试")
        }
        return res.send({
            status:0,
            message:"删除文章分类成功",
            data:result[0]
        })
    })
}
/**
 * 批量删除文件分类
 * @param {*} req 
 * @param {*} res 
 */
exports.delArtcatesById=(req,res)=>{
    var arr = JSON.parse(req.body.delArr);
    var ids = ""
    for (var i = 0; i < arr.length; i++) {
        ids +=   arr[i] + ','
    }
    var str = ids.substr(0,ids.length-1)
    var  delArrArticakCateSql = `update ev_artical_cate set isDelete=1 where id in(${str})`
    db.query(delArrArticakCateSql,(err,result)=>{
        if (err) {
            return res.cc(err)
        }
        return res.send({
            status:0,
            message:"批量删除成功！",
        })
    })
}
/**
 * 根据id更新文章分类数据
 */
exports.updateArtcateById=(req,res)=>{
    db.query(isExitArticalSql,[req.body.id,req.body.name,req.body.alias],(err,result)=>{
        if (err) {
            return res.cc(err)
        }
        // length == 2
        if (result.length==2) {
            return res.cc("分类名称和分类别名被占用，请更换后重试！");
        }
        if (result.length==1&&result[0].name==req.body.name&&result[0].alias==req.body.alias) {
            return res.cc("分类名称和分类别名被占用，请更换后重试！");
        }
        if (result.length==1&&result[0].name==req.body.name) {
            return res.cc("分类名称被占用，请更换后重试")
        }
        if (result.length==1&&result[0].alias==req.body.alias) {
            return res.cc("分类别名被占用，请更换后重试")
        }
        db.query(updateArtcateByIdSql,[req.body,req.body.id],(err,result)=>{
            if (err) {
                return res.cc(err)
            }
            if (result.affectedRows !== 1) {
                return res.cc("更新文章分类失败，请稍后再试")
            }
            return res.send({
                status:0,
                message:"跟新分类列表成功！",
            })
        })
    })
}

/**
 * 下载图片
 */
exports.downLoadImg=(req,res)=>{
    const url = req.query.url;
    // console.log(url);
    console.log(req.query.url);
    url?res.download(`upload/${url}`):res.cc("下载失败，请稍后重试");
}
